package com.ruoyi.merchant.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.merchant.domain.CustomerHistoryMedicalDisease;
import com.ruoyi.merchant.dto.CustomerHistoryMedicalDto;
import com.ruoyi.merchant.mapper.CustomerHistoryMedicalDiseaseMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.merchant.mapper.CustomerHistoryMedicalMapper;
import com.ruoyi.merchant.domain.CustomerHistoryMedical;
import com.ruoyi.merchant.service.ICustomerHistoryMedicalService;
import org.springframework.util.CollectionUtils;

/**
 * 客户病史习惯Service业务层处理
 * 
 * @author chengxin.ju
 * @date 2024-04-09
 */
@Service
public class CustomerHistoryMedicalServiceImpl implements ICustomerHistoryMedicalService 
{
    @Autowired
    private CustomerHistoryMedicalMapper customerHistoryMedicalMapper;
    @Autowired
    private CustomerHistoryMedicalDiseaseMapper customerHistoryMedicalDiseaseMapper;

    /**
     * 查询客户病史习惯
     *
     * @param ID 客户病史习惯主键
     * @return 客户病史习惯
     */
    @Override
    public CustomerHistoryMedicalDto selectCustomerHistoryMedicalByID(String id)
    {
        CustomerHistoryMedical customerHistoryMedical = customerHistoryMedicalMapper.selectCustomerHistoryMedicalById(id);
        if (customerHistoryMedical == null){
            return null;
        }
        CustomerHistoryMedicalDto customerHistoryMedicalDto = new CustomerHistoryMedicalDto();
        BeanUtils.copyProperties(customerHistoryMedical,customerHistoryMedicalDto);

        CustomerHistoryMedicalDisease customerHistoryMedicalDisease = new CustomerHistoryMedicalDisease();
        customerHistoryMedicalDisease.setHistoryId(customerHistoryMedical.getId());
        List<CustomerHistoryMedicalDisease> customerHistoryMedicalDiseaseList = customerHistoryMedicalDiseaseMapper.selectCustomerHistoryMedicalDiseaseList(customerHistoryMedicalDisease);
        if (CollectionUtils.isEmpty(customerHistoryMedicalDiseaseList)){
            return customerHistoryMedicalDto;
        }
        List<String> collect = customerHistoryMedicalDiseaseList.stream().map(CustomerHistoryMedicalDisease::getDiseaseType).collect(Collectors.toList());
        customerHistoryMedicalDto.setDiseaseTypeList(collect);
        return customerHistoryMedicalDto;
    }

    /**
     * 查询客户病史习惯列表
     * 
     * @param customerHistoryMedical 客户病史习惯
     * @return 客户病史习惯
     */
    @Override
    public List<CustomerHistoryMedical> selectCustomerHistoryMedicalList(CustomerHistoryMedical customerHistoryMedical)
    {
        return customerHistoryMedicalMapper.selectCustomerHistoryMedicalList(customerHistoryMedical);
    }


    /**
     * 新增客户病史习惯
     * 
     * @param customerHistoryMedicalDto 客户病史习惯
     * @return 结果
     */
    @Override
    public int insertCustomerHistoryMedical(CustomerHistoryMedicalDto customerHistoryMedicalDto)
    {

        int i = customerHistoryMedicalMapper.insertCustomerHistoryMedical(customerHistoryMedicalDto);
        if (CollectionUtils.isEmpty(customerHistoryMedicalDto.getDiseaseTypeList())){
            return i;
        }
        List<CustomerHistoryMedicalDisease>  customerHistoryMedicalDiseaseList = customerHistoryMedicalDto.getDiseaseTypeList().stream().map(item->{

            CustomerHistoryMedicalDisease customerHistoryMedicalDisease = new CustomerHistoryMedicalDisease();
            customerHistoryMedicalDisease.setId(IdUtils.fastSimpleUUID());
            customerHistoryMedicalDisease.setHistoryId(customerHistoryMedicalDto.getId());
            customerHistoryMedicalDisease.setCustomerId(customerHistoryMedicalDto.getCustomerId());
            customerHistoryMedicalDisease.setDiseaseType(item);
            //app 端暂时不处理其他。
            return customerHistoryMedicalDisease;
        }).collect(Collectors.toList());

        for (CustomerHistoryMedicalDisease customerHistoryMedicalDisease: customerHistoryMedicalDiseaseList ) {
            customerHistoryMedicalDiseaseMapper.insertCustomerHistoryMedicalDisease(customerHistoryMedicalDisease);
        }

        return i;
    }

    /**
     * 修改客户病史习惯
     * 
     * @param customerHistoryMedicalDto 客户病史习惯
     * @return 结果
     */
    @Override
    public int updateCustomerHistoryMedical(CustomerHistoryMedicalDto customerHistoryMedicalDto)
    {
        int i = customerHistoryMedicalMapper.updateCustomerHistoryMedical(customerHistoryMedicalDto);
        if (CollectionUtils.isEmpty(customerHistoryMedicalDto.getDiseaseTypeList())){
            return i;
        }
        List<CustomerHistoryMedicalDisease>  customerHistoryMedicalDiseaseList = customerHistoryMedicalDto.getDiseaseTypeList().stream().map(item->{
            CustomerHistoryMedicalDisease customerHistoryMedicalDisease = new CustomerHistoryMedicalDisease();
            customerHistoryMedicalDisease.setId(IdUtils.fastSimpleUUID());
            customerHistoryMedicalDisease.setHistoryId(customerHistoryMedicalDto.getId());
            customerHistoryMedicalDisease.setCustomerId(customerHistoryMedicalDto.getCustomerId());
            customerHistoryMedicalDisease.setDiseaseType(item);
            //app 端暂时不处理其他。
            return customerHistoryMedicalDisease;
        }).collect(Collectors.toList());

        customerHistoryMedicalDiseaseMapper.deleteCustomerHistoryMedicalDiseaseByHistoryId(customerHistoryMedicalDto.getId());

        for (CustomerHistoryMedicalDisease customerHistoryMedicalDisease: customerHistoryMedicalDiseaseList ) {
            customerHistoryMedicalDiseaseMapper.updateCustomerHistoryMedicalDisease(customerHistoryMedicalDisease);
        }
        return i;
    }

    /**
     * 批量删除客户病史习惯
     * 
     * @param IDs 需要删除的客户病史习惯主键
     * @return 结果
     */
    @Override
    public int deleteCustomerHistoryMedicalByIDs(String[] IDs)
    {
        return customerHistoryMedicalMapper.deleteCustomerHistoryMedicalByIDs(IDs);
    }

    /**
     * 删除客户病史习惯信息
     * 
     * @param ID 客户病史习惯主键
     * @return 结果
     */
    @Override
    public int deleteCustomerHistoryMedicalByID(String ID)
    {
        return customerHistoryMedicalMapper.deleteCustomerHistoryMedicalByID(ID);
    }
}
